/**
 * 
 */
package com.mc.qrcode;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.qrcode.QRCodeMultiReader;

/**
 * 二维码
 * @author w.weizhen
 *
 */
public class QRReader {
	private static String path0 = "G:\\workspace\\file\\meitu_1.jpg";
    private static String path1 = "G:\\workspace\\file\\meitu_2.jpg";
    /**
     * @param args
     */
    public static void main(String[] args) {
        
    }
    public static void zxing0() {
        File file = new File(path0);
        try {
            BufferedImage image = ImageIO.read(file);
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 解码
            // 输出相关的二维码信息
            System.out.println("解析结果：" + result.toString());
            System.out.println("二维码格式类型：" + result.getBarcodeFormat());
            System.out.println("二维码文本内容：" + result.getText());
            image.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void zxing1() {
        File file = new File(path1);
        try {
            BufferedImage bufferedImage = ImageIO.read(file);
            QRCodeMultiReader qrCodeMultiReader = new QRCodeMultiReader();
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); // 优化精度
            hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); // 复杂模式，开启PURE_BARCODE模式； 带图片LOGO的解码方案
            hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE);// 指定识别的二维码格式
            Result[] results = qrCodeMultiReader.decodeMultiple(binaryBitmap, hints);
            for (int i = 0; i < results.length; i++) {
                Result result = results[i];
                // 输出相关的二维码信息
                System.out.println("解析结果：" + result.toString());
                System.out.println("二维码格式类型：" + result.getBarcodeFormat());
                System.out.println("二维码文本内容：" + result.getText());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
